<?php

namespace App\Library\Service;

use App\Library\Logic\AddressLogic;
use App\Library\Logic\AuctionBailLogic;
use App\Library\Logic\AuctionOrderLogic;
use App\Library\Logic\OperateLogLogic;
use App\Library\Logic\ShopUserAddressLogic;
use App\Library\Requests\Auction\AuctionOrderAddressRequest;
use App\Library\Requests\Auction\AuctionOrderPageRequest;
use App\Library\Requests\Base\BaseDeliveryRequest;
use App\Library\Requests\Base\SerialNumberRequest;
use App\Library\Service\Base\BaseLogisticService;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;

class AuctionOrderService extends BaseService
{
    /**
     * desc
     *
     * @return AuctionOrderLogic
     */
    private function logic(): AuctionOrderLogic
    {
        if (empty($this->logic)) $this->logic = new AuctionOrderLogic;
        return $this->logic;
    }

    /**
     * 发货
     * @param BaseDeliveryRequest $request
     * @return JsonResponse
     * @throws GuzzleException
     */
    public function delivery(BaseDeliveryRequest $request): JsonResponse
    {
        $order = $this->logic()->findBySerialNumber($request);

        if (is_null($order)) return fail(433);
        else if ($order->status_pay === 0) return fail(614);
        $order->status_deliver = 1;
        $order->deliver_at = now();
        $order->status = 3;
        $order->save();
        LogisticsService::delivery($order->id, $request->expressNo, 0);
        OperateLogLogic::add();
        return success();
    }

    /**
     * 物流信息查询
     * @param SerialNumberRequest $request
     * @return JsonResponse
     * @throws GuzzleException
     */
    public function logistics(SerialNumberRequest $request): JsonResponse
    {
        $order = $this->logic()->findBySerialNumber($request);
        if (is_null($order)) return fail(433);
        else if ($order->status_deliver !== 1) return fail(615);
        else if ($order->status_cancel === 1) return fail(434);
        if ($order->logistics->status_sign === 1) return success($order->logistics->info);
        else return success(BaseLogisticService::orderLogistics($order));
    }

    /**
     * 拍卖订单填写地址
     * @param AuctionOrderAddressRequest $request
     * @return JsonResponse
     *
     */
    public function address(AuctionOrderAddressRequest $request): JsonResponse
    {
      /*  $address = ShopUserAddressLogic::query()->where([
            'ua_id' => $request->addressId,
            'user_id' => USERID
        ])->first();*/
        $address = AddressLogic::query()
            ->select(
                'id as au_id',
                'contacts as ua_contacts',
                'mobile as ua_contacts_mobile',
                'address as ua_address',
                'full_address as ua_address_full',
            )
            ->where([
            'id' => $request->addressId,
            'user_id' => USERID
        ])->first();
        if (is_null($address)) return fail(433);
        $order = AuctionOrderLogic::findBySerialNumber($request);
        if (is_null($order)) return fail(439);
        $order->receive_info = json_encode($address);
        $order->save();
        return success();
    }

    /**
     * 确认收货
     * @param SerialNumberRequest $request
     * @return JsonResponse
     *
     */
    public function confirmReceipt(SerialNumberRequest $request): JsonResponse
    {
        $order = $this->logic()->findBySerialNumber($request);
        if (is_null($order)) return fail(433);
        else if ($order->status_deliver !== 1) return fail(615);
        else if ($order->status_pay !== 1) return fail(614);

        $order->status = 4;
        $order->status_complete = 1;
        $order->confirm_at = now();
        $order->status_sign = 1;
        $order->save();

        return success();
    }

    /**
     * 退保证金
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function refundBail(int $id): JsonResponse
    {
        $order = $this->logic()->detail($id);
        if (is_null($order) || is_null($order->pay_at)) return fail();
        $serialNumber = AuctionBailLogic::query()->where(humpToLine([
            'auctionId' => $order->auction_id,
            'userId' => $order->user_id,
            'statusPay' => 1,
        ]))->whereNotNull('pay_at')->value('serial_number');
        if (is_null($serialNumber)) return fail();
        mq('auctionBailRefund', $serialNumber);
        OperateLogLogic::add();
        return success();
    }

    /**
     * 用户删除拍卖订单
     * @param SerialNumberRequest $request
     * @return JsonResponse
     *
     */
    public function userDelete(SerialNumberRequest $request): JsonResponse
    {
        $order = $this->logic()->findBySerialNumber($request);
        if (is_null($order)) return fail(433);
        else if ($order->user_id !== USERID) return fail(433);
        else if ($order->status !== 4) return fail(617);
        $order->user_deleted_at = now();
        $order->save();
        return success();
    }

    /**
     * 商家删除拍卖订单
     * @param SerialNumberRequest $request
     * @return JsonResponse
     *
     */
    public function sellerDelete(SerialNumberRequest $request): JsonResponse
    {
        $order = $this->logic()->findBySerialNumber($request);
        if (is_null($order)) return fail(433);
        else if ($order->seller_id !== USERID) return fail(433);
        else if ($order->status !== 4) return fail(617);
        $order->seller_deleted_at = now();
        $order->save();
        OperateLogLogic::add();
        return success();
    }

    /**
     * 拍卖订单分页-用户
     * @param AuctionOrderPageRequest $request
     * @return JsonResponse
     *
     */
    public function userPaging(AuctionOrderPageRequest $request): JsonResponse
    {
        return success($this->logic()->userPaging($request));
    }

    /**
     * 拍卖订单分页-商家
     * @param AuctionOrderPageRequest $request
     * @return JsonResponse
     */
    public function sellerPaging(AuctionOrderPageRequest $request): JsonResponse
    {
        return success($this->logic()->sellerPaging($request));
    }

    /**
     * detail
     * @param SerialNumberRequest $request
     * @return JsonResponse
     */
    public function detail(SerialNumberRequest $request): JsonResponse
    {
        $item = $this->logic()->auctionOrderDetail($request);
        if (is_null($item)) return fail();
        else if ($item->auction->bail_price * 1 > 0) {
            $item->my_bail_price = DB::table('auction_bail')->where([
                'user_id' => USERID,
                'auction_id' => $item->id,
                'status_pay' => 1,
            ])->value('pay_price');
        }
        return success($item);
    }
}
